{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___\n",
    "\n",
    "<a href='https://www.udemy.com/user/joseportilla/'><img src='../Pierian_Data_Logo.png'/></a>\n",
    "___\n",
    "<center><em>Content Copyright by Pierian Data</em></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Warmup Project Exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple War Game\n",
    "\n",
    "Before we launch in to the OOP Milestone 2 Project, let's walk through together on using OOP for a more robust and complex application, such as a game. We will use Python OOP to simulate a simplified version of the game war. Two players will each start off with half the deck, then they each remove a card, compare which card has the highest value, and the player with the higher card wins both cards. In the event of a time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Card Class\n",
    "\n",
    "### Creating a Card Class with outside variables\n",
    "\n",
    "Here we will use some outside variables that we know don't change regardless of the situation, such as a deck of cards. Regardless of what round,match, or game we're playing, we'll still need the same deck of cards."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# We'll use this later\n",
    "import random "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
    "ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
    "values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, \n",
    "            'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Card:\n",
    "    \n",
    "    def __init__(self,suit,rank):\n",
    "        self.suit = suit\n",
    "        self.rank = rank\n",
    "        self.value = values[rank]\n",
    "        \n",
    "    def __str__(self):\n",
    "        return self.rank + ' of ' + self.suit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an example card"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hearts'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "suits[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Two'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ranks[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "two_hearts = Card(suits[0],ranks[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Card at 0x1dfaff6b898>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two_hearts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Two of Hearts\n"
     ]
    }
   ],
   "source": [
    "print(two_hearts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Two'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two_hearts.rank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two_hearts.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values[two_hearts.rank]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deck Class\n",
    "\n",
    "### Using a class within another class\n",
    "\n",
    "We just created a single card, but how can we create an entire Deck of cards? Let's explore doing this with a class that utilizes the Card class."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A Deck will be made up of multiple Cards. Which mean's we will actually use the Card class within the \\_\\_init__ of the Deck class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Deck:\n",
    "    \n",
    "    def __init__(self):\n",
    "        # Note this only happens once upon creation of a new Deck\n",
    "        self.all_cards = [] \n",
    "        for suit in suits:\n",
    "            for rank in ranks:\n",
    "                # This assumes the Card class has already been defined!\n",
    "                self.all_cards.append(Card(suit,rank))\n",
    "                \n",
    "    def shuffle(self):\n",
    "        # Note this doesn't return anything\n",
    "        random.shuffle(self.all_cards)\n",
    "        \n",
    "    def deal_one(self):\n",
    "        # Note we remove one card from the list of all_cards\n",
    "        return self.all_cards.pop()        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Deck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mydeck = Deck()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "52"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(mydeck.all_cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Card at 0x1dfaff269e8>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydeck.all_cards[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Two of Hearts\n"
     ]
    }
   ],
   "source": [
    "print(mydeck.all_cards[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mydeck.shuffle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Five of Spades\n"
     ]
    }
   ],
   "source": [
    "print(mydeck.all_cards[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "my_card = mydeck.deal_one()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "King of Clubs\n"
     ]
    }
   ],
   "source": [
    "print(my_card)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Player Class\n",
    "\n",
    "Let's create a Player Class, a player should be able to hold instances of Cards, they should also be able to remove and add them from their hand. We want the Player class to be flexible enough to add one card, or many cards so we'll use a simple if check to keep it all in the same method."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll keep this all in mind as we create the methods for the Player class.\n",
    "\n",
    "### Player Class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Player:\n",
    "    \n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "        # A new player has no cards\n",
    "        self.all_cards = [] \n",
    "        \n",
    "    def remove_one(self):\n",
    "        # Note we remove one card from the list of all_cards\n",
    "        # We state 0 to remove from the \"top\" of the deck\n",
    "        # We'll imagine index -1 as the bottom of the deck\n",
    "        return self.all_cards.pop(0)\n",
    "    \n",
    "    def add_cards(self,new_cards):\n",
    "        if type(new_cards) == type([]):\n",
    "            self.all_cards.extend(new_cards)\n",
    "        else:\n",
    "            self.all_cards.append(new_cards)\n",
    "    \n",
    "    \n",
    "    def __str__(self):\n",
    "        return f'Player {self.name} has {len(self.all_cards)} cards.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jose = Player(\"Jose\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Player at 0x1dfaff8b940>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player Jose has 0 cards.\n"
     ]
    }
   ],
   "source": [
    "print(jose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Card at 0x1dfaff6b898>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two_hearts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jose.add_cards(two_hearts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player Jose has 1 cards.\n"
     ]
    }
   ],
   "source": [
    "print(jose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jose.add_cards([two_hearts,two_hearts,two_hearts])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player Jose has 4 cards.\n"
     ]
    }
   ],
   "source": [
    "print(jose)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## War Game Logic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "player_one = Player(\"One\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "player_two = Player(\"Two\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup New Game"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "new_deck = Deck()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "new_deck.shuffle()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Split the Deck between players"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26.0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(new_deck.all_cards)/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for x in range(26):\n",
    "    player_one.add_cards(new_deck.deal_one())\n",
    "    player_two.add_cards(new_deck.deal_one())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(new_deck.all_cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(player_one.all_cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(player_two.all_cards)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Play the Game"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "game_on = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round 1\n",
      "Round 2\n",
      "Round 3\n",
      "Round 4\n",
      "Round 5\n",
      "Round 6\n",
      "Round 7\n",
      "Round 8\n",
      "Round 9\n",
      "Round 10\n",
      "Round 11\n",
      "Round 12\n",
      "Round 13\n",
      "Round 14\n",
      "Round 15\n",
      "Round 16\n",
      "Round 17\n",
      "Round 18\n",
      "Round 19\n",
      "Round 20\n",
      "Round 21\n",
      "Round 22\n",
      "Round 23\n",
      "Round 24\n",
      "Round 25\n",
      "Round 26\n",
      "Round 27\n",
      "Player One out of cards! Game Over\n"
     ]
    }
   ],
   "source": [
    "round_num = 0\n",
    "while game_on:\n",
    "    \n",
    "    round_num += 1\n",
    "    print(f\"Round {round_num}\")\n",
    "    \n",
    "    # Check to see if a player is out of cards:\n",
    "    if len(player_one.all_cards) == 0:\n",
    "        print(\"Player One out of cards! Game Over\")\n",
    "        print(\"Player Two Wins!\")\n",
    "        game_on = False\n",
    "        break\n",
    "        \n",
    "    if len(player_two.all_cards) == 0:\n",
    "        print(\"Player Two out of cards! Game Over\")\n",
    "        print(\"Player One Wins!\")\n",
    "        game_on = False\n",
    "        break\n",
    "    \n",
    "    # Otherwise, the game is still on!\n",
    "    \n",
    "    # Start a new round and reset current cards \"on the table\"\n",
    "    player_one_cards = []\n",
    "    player_one_cards.append(player_one.remove_one())\n",
    "    \n",
    "    player_two_cards = []\n",
    "    player_two_cards.append(player_two.remove_one())\n",
    "    \n",
    "    at_war = True\n",
    "\n",
    "    while at_war:\n",
    "\n",
    "\n",
    "        if player_one_cards[-1].value > player_two_cards[-1].value:\n",
    "\n",
    "            # Player One gets the cards\n",
    "            player_one.add_cards(player_one_cards)\n",
    "            player_one.add_cards(player_two_cards)\n",
    "            \n",
    "            \n",
    "            # No Longer at \"war\" , time for next round\n",
    "            at_war = False\n",
    "        \n",
    "        # Player Two Has higher Card\n",
    "        elif player_one_cards[-1].value < player_two_cards[-1].value:\n",
    "\n",
    "            # Player Two gets the cards\n",
    "            player_two.add_cards(player_one_cards)\n",
    "            player_two.add_cards(player_two_cards)\n",
    "            \n",
    "            # No Longer at \"war\" , time for next round\n",
    "            at_war = False\n",
    "\n",
    "        else:\n",
    "            print('WAR!')\n",
    "            # This occurs when the cards are equal.\n",
    "            # We'll grab another card each and continue the current war.\n",
    "            \n",
    "            # First check to see if player has enough cards\n",
    "            \n",
    "            # Check to see if a player is out of cards:\n",
    "            if len(player_one.all_cards) < 5:\n",
    "                print(\"Player One unable to play war! Game Over at War\")\n",
    "                print(\"Player Two Wins! Player One Loses!\")\n",
    "                game_on = False\n",
    "                break\n",
    "\n",
    "            elif len(player_two.all_cards) < 5:\n",
    "                print(\"Player Two unable to play war! Game Over at War\")\n",
    "                print(\"Player One Wins! Player One Loses!\")\n",
    "                game_on = False\n",
    "                break\n",
    "            # Otherwise, we're still at war, so we'll add the next cards\n",
    "            else:\n",
    "                for num in range(5):\n",
    "                    player_one_cards.append(player_one.remove_one())\n",
    "                    player_two_cards.append(player_two.remove_one())\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Game Setup in One Cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "player_one = Player(\"One\")\n",
    "player_two = Player(\"Two\")\n",
    "\n",
    "new_deck = Deck()\n",
    "new_deck.shuffle()\n",
    "\n",
    "for x in range(26):\n",
    "    player_one.add_cards(new_deck.deal_one())\n",
    "    player_two.add_cards(new_deck.deal_one())\n",
    "    \n",
    "game_on = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round 1\n",
      "Round 2\n",
      "WAR!\n",
      "Round 3\n",
      "WAR!\n",
      "WAR!\n",
      "Round 4\n",
      "WAR!\n",
      "Round 5\n",
      "Round 6\n",
      "Round 7\n",
      "Round 8\n",
      "Round 9\n",
      "Round 10\n",
      "Round 11\n",
      "Round 12\n",
      "Round 13\n",
      "WAR!\n",
      "Round 14\n",
      "Round 15\n",
      "WAR!\n",
      "Round 16\n",
      "Round 17\n",
      "Round 18\n",
      "Round 19\n",
      "Round 20\n",
      "Round 21\n",
      "Round 22\n",
      "Round 23\n",
      "Round 24\n",
      "Round 25\n",
      "Round 26\n",
      "Round 27\n",
      "Round 28\n",
      "Round 29\n",
      "WAR!\n",
      "Round 30\n",
      "Round 31\n",
      "Round 32\n",
      "WAR!\n",
      "Round 33\n",
      "Round 34\n",
      "Round 35\n",
      "Round 36\n",
      "Round 37\n",
      "Round 38\n",
      "Round 39\n",
      "Round 40\n",
      "Round 41\n",
      "Round 42\n",
      "WAR!\n",
      "WAR!\n",
      "Round 43\n",
      "Round 44\n",
      "Round 45\n",
      "Round 46\n",
      "Round 47\n",
      "Round 48\n",
      "Round 49\n",
      "Round 50\n",
      "Round 51\n",
      "Round 52\n",
      "Round 53\n",
      "Round 54\n",
      "Round 55\n",
      "Round 56\n",
      "Round 57\n",
      "Round 58\n",
      "Round 59\n",
      "Round 60\n",
      "Round 61\n",
      "Round 62\n",
      "Round 63\n",
      "Round 64\n",
      "Round 65\n",
      "Round 66\n",
      "Round 67\n",
      "Round 68\n",
      "Round 69\n",
      "Round 70\n",
      "Round 71\n",
      "Round 72\n",
      "Round 73\n",
      "Round 74\n",
      "Round 75\n",
      "Round 76\n",
      "Round 77\n",
      "Round 78\n",
      "Round 79\n",
      "Round 80\n",
      "Round 81\n",
      "Round 82\n",
      "Round 83\n",
      "Round 84\n",
      "Round 85\n",
      "Round 86\n",
      "Round 87\n",
      "Round 88\n",
      "Round 89\n",
      "Round 90\n",
      "Round 91\n",
      "Round 92\n",
      "Round 93\n",
      "Round 94\n",
      "Round 95\n",
      "Round 96\n",
      "Round 97\n",
      "Round 98\n",
      "Round 99\n",
      "Round 100\n",
      "Round 101\n",
      "Round 102\n",
      "Round 103\n",
      "Round 104\n",
      "Round 105\n",
      "Round 106\n",
      "Round 107\n",
      "Round 108\n",
      "WAR!\n",
      "Round 109\n",
      "Round 110\n",
      "Round 111\n",
      "Round 112\n",
      "Round 113\n",
      "Round 114\n",
      "Round 115\n",
      "Round 116\n",
      "Round 117\n",
      "Round 118\n",
      "Round 119\n",
      "Round 120\n",
      "Round 121\n",
      "Round 122\n",
      "Round 123\n",
      "Round 124\n",
      "Round 125\n",
      "Round 126\n",
      "Round 127\n",
      "Round 128\n",
      "Round 129\n",
      "Round 130\n",
      "Round 131\n",
      "Round 132\n",
      "Round 133\n",
      "Round 134\n",
      "WAR!\n",
      "Round 135\n",
      "Round 136\n",
      "WAR!\n",
      "Round 137\n",
      "WAR!\n",
      "Round 138\n",
      "Round 139\n",
      "Round 140\n",
      "Round 141\n",
      "Round 142\n",
      "Round 143\n",
      "Round 144\n",
      "Round 145\n",
      "Round 146\n",
      "Round 147\n",
      "Round 148\n",
      "Round 149\n",
      "Round 150\n",
      "Round 151\n",
      "Round 152\n",
      "Round 153\n",
      "Round 154\n",
      "Round 155\n",
      "Round 156\n",
      "Round 157\n",
      "Round 158\n",
      "Round 159\n",
      "Round 160\n",
      "Round 161\n",
      "Round 162\n",
      "Round 163\n",
      "Round 164\n",
      "Round 165\n",
      "Round 166\n",
      "Round 167\n",
      "Round 168\n",
      "Round 169\n",
      "Round 170\n",
      "Round 171\n",
      "Round 172\n",
      "Round 173\n",
      "Round 174\n",
      "Round 175\n",
      "Round 176\n",
      "Round 177\n",
      "Round 178\n",
      "Round 179\n",
      "Round 180\n",
      "Round 181\n",
      "Round 182\n",
      "Round 183\n",
      "Round 184\n",
      "Round 185\n",
      "Round 186\n",
      "Round 187\n",
      "Round 188\n",
      "Round 189\n",
      "Round 190\n",
      "Round 191\n",
      "Round 192\n",
      "Round 193\n",
      "Round 194\n",
      "Round 195\n",
      "Round 196\n",
      "Round 197\n",
      "Round 198\n",
      "Round 199\n",
      "Round 200\n",
      "Round 201\n",
      "Round 202\n",
      "Round 203\n",
      "Round 204\n",
      "Round 205\n",
      "Round 206\n",
      "Round 207\n",
      "Round 208\n",
      "Round 209\n",
      "Round 210\n",
      "Round 211\n",
      "Round 212\n",
      "Round 213\n",
      "Round 214\n",
      "Round 215\n",
      "Round 216\n",
      "Round 217\n",
      "Round 218\n",
      "Round 219\n",
      "Round 220\n",
      "Round 221\n",
      "Round 222\n",
      "Round 223\n",
      "WAR!\n",
      "Round 224\n",
      "Round 225\n",
      "Round 226\n",
      "Round 227\n",
      "Round 228\n",
      "Round 229\n",
      "Round 230\n",
      "Round 231\n",
      "Round 232\n",
      "Round 233\n",
      "Round 234\n",
      "Round 235\n",
      "Round 236\n",
      "Round 237\n",
      "Round 238\n",
      "Round 239\n",
      "Round 240\n",
      "Round 241\n",
      "Round 242\n",
      "Round 243\n",
      "Round 244\n",
      "Round 245\n",
      "Round 246\n",
      "Round 247\n",
      "Round 248\n",
      "Round 249\n",
      "Round 250\n",
      "Round 251\n",
      "Round 252\n",
      "Round 253\n",
      "Round 254\n",
      "Round 255\n",
      "Round 256\n",
      "Round 257\n",
      "WAR!\n",
      "Round 258\n",
      "Round 259\n",
      "Round 260\n",
      "Round 261\n",
      "Round 262\n",
      "Round 263\n",
      "Round 264\n",
      "Round 265\n",
      "Round 266\n",
      "Round 267\n",
      "Round 268\n",
      "Round 269\n",
      "Round 270\n",
      "Round 271\n",
      "Round 272\n",
      "Round 273\n",
      "Round 274\n",
      "Round 275\n",
      "Round 276\n",
      "Round 277\n",
      "Round 278\n",
      "Round 279\n",
      "Round 280\n",
      "Round 281\n",
      "Round 282\n",
      "Round 283\n",
      "Round 284\n",
      "Round 285\n",
      "Round 286\n",
      "Round 287\n",
      "Round 288\n",
      "Round 289\n",
      "Round 290\n",
      "Round 291\n",
      "Round 292\n",
      "Round 293\n",
      "Round 294\n",
      "Round 295\n",
      "Round 296\n",
      "Round 297\n",
      "Round 298\n",
      "Round 299\n",
      "Round 300\n",
      "Round 301\n",
      "Round 302\n",
      "Round 303\n",
      "Round 304\n",
      "Round 305\n",
      "Round 306\n",
      "Round 307\n",
      "WAR!\n",
      "Round 308\n",
      "Round 309\n",
      "Round 310\n",
      "Round 311\n",
      "Round 312\n",
      "Round 313\n",
      "Round 314\n",
      "Round 315\n",
      "WAR!\n",
      "Round 316\n",
      "Round 317\n",
      "Round 318\n",
      "Round 319\n",
      "Round 320\n",
      "Round 321\n",
      "Round 322\n",
      "Round 323\n",
      "Round 324\n",
      "Round 325\n",
      "Round 326\n",
      "Round 327\n",
      "Round 328\n",
      "Round 329\n",
      "Round 330\n",
      "Round 331\n",
      "Round 332\n",
      "Round 333\n",
      "Round 334\n",
      "Round 335\n",
      "Round 336\n",
      "Round 337\n",
      "Round 338\n",
      "Round 339\n",
      "Round 340\n",
      "Round 341\n",
      "Round 342\n",
      "Round 343\n",
      "Round 344\n",
      "Round 345\n",
      "Round 346\n",
      "Round 347\n",
      "Round 348\n",
      "Round 349\n",
      "WAR!\n",
      "Round 350\n",
      "WAR!\n",
      "Player Two unable to play war! Game Over at War\n",
      "Player One Wins! Player One Loses!\n"
     ]
    }
   ],
   "source": [
    "round_num = 0\n",
    "while game_on:\n",
    "    \n",
    "    round_num += 1\n",
    "    print(f\"Round {round_num}\")\n",
    "    \n",
    "    # Check to see if a player is out of cards:\n",
    "    if len(player_one.all_cards) == 0:\n",
    "        print(\"Player One out of cards! Game Over\")\n",
    "        print(\"Player Two Wins!\")\n",
    "        game_on = False\n",
    "        break\n",
    "        \n",
    "    if len(player_two.all_cards) == 0:\n",
    "        print(\"Player Two out of cards! Game Over\")\n",
    "        print(\"Player One Wins!\")\n",
    "        game_on = False\n",
    "        break\n",
    "    \n",
    "    # Otherwise, the game is still on!\n",
    "    \n",
    "    # Start a new round and reset current cards \"on the table\"\n",
    "    player_one_cards = []\n",
    "    player_one_cards.append(player_one.remove_one())\n",
    "    \n",
    "    player_two_cards = []\n",
    "    player_two_cards.append(player_two.remove_one())\n",
    "    \n",
    "    at_war = True\n",
    "\n",
    "    while at_war:\n",
    "\n",
    "\n",
    "        if player_one_cards[-1].value > player_two_cards[-1].value:\n",
    "\n",
    "            # Player One gets the cards\n",
    "            player_one.add_cards(player_one_cards)\n",
    "            player_one.add_cards(player_two_cards)\n",
    "            \n",
    "            \n",
    "            # No Longer at \"war\" , time for next round\n",
    "            at_war = False\n",
    "        \n",
    "        # Player Two Has higher Card\n",
    "        elif player_one_cards[-1].value < player_two_cards[-1].value:\n",
    "\n",
    "            # Player Two gets the cards\n",
    "            player_two.add_cards(player_one_cards)\n",
    "            player_two.add_cards(player_two_cards)\n",
    "            \n",
    "            # No Longer at \"war\" , time for next round\n",
    "            at_war = False\n",
    "\n",
    "        else:\n",
    "            print('WAR!')\n",
    "            # This occurs when the cards are equal.\n",
    "            # We'll grab another card each and continue the current war.\n",
    "            \n",
    "            # First check to see if player has enough cards\n",
    "            \n",
    "            # Check to see if a player is out of cards:\n",
    "            if len(player_one.all_cards) < 5:\n",
    "                print(\"Player One unable to play war! Game Over at War\")\n",
    "                print(\"Player Two Wins! Player One Loses!\")\n",
    "                game_on = False\n",
    "                break\n",
    "\n",
    "            elif len(player_two.all_cards) < 5:\n",
    "                print(\"Player Two unable to play war! Game Over at War\")\n",
    "                print(\"Player One Wins! Player One Loses!\")\n",
    "                game_on = False\n",
    "                break\n",
    "            # Otherwise, we're still at war, so we'll add the next cards\n",
    "            else:\n",
    "                for num in range(5):\n",
    "                    player_one_cards.append(player_one.remove_one())\n",
    "                    player_two_cards.append(player_two.remove_one())\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(player_one.all_cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(player_two.all_cards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ace of Diamonds\n"
     ]
    }
   ],
   "source": [
    "print(player_one_cards[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Four of Hearts\n"
     ]
    }
   ],
   "source": [
    "print(player_two_cards[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Great Work!\n",
    "\n",
    "Other links that may interest you:\n",
    "* https://www.reddit.com/r/learnpython/comments/7ay83p/war_card_game/\n",
    "* https://codereview.stackexchange.com/questions/131174/war-card-game-using-classes\n",
    "* https://gist.github.com/damianesteban/6896120\n",
    "* https://lethain.com/war-card-game-in-python/\n",
    "* https://hectorpefo.github.io/2017-09-13-Card-Wars/\n",
    "* https://www.wimpyprogrammer.com/the-statistics-of-war-the-card-game"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
